package com.byd.dms.controller;

import com.byd.dms.entity.User;
import com.byd.dms.util.JsonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * https://mp.weixin.qq.com/s/RAqRKZJqsJ78HRrJg71R1g
 */
@RequestMapping("/user")
@Controller
public class UserController {
    
    @Autowired
    private HttpServletRequest request;
    
    
    @RequestMapping("/save")
    @ResponseBody
    public User saveUser() {
        String city = request.getParameter("city");
        System.out.println("city = " + city);
        
        String user = request.getParameter("user");
        System.out.println("user = " + user);
        
      
        
        
        return null;
    }
    
    /**
     * Content-Type: text/html;charset=UTF-8
     *
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/json1", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String json1() throws JsonProcessingException {
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user = new User("秦疆1号", 3, "男");
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(user);
        
        System.out.println(str);
        //由于@ResponseBody注解，这里会将str转成json格式返回；十分方便
        return str;
    }
    
    /**
     * Content-Type: application/json
     *
     * @return
     */
    @RequestMapping("/jsona")
    @ResponseBody
    public User jsona() {
        User user = new User("秦疆1号", 3, "男");
        return user;
    }
    
    @RequestMapping("/json2")
    @ResponseBody
    public String json2() throws JsonProcessingException {
        
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(list);
        return str;
    }
    
    
    @RequestMapping("/json3")
    @ResponseBody
    public String json3() throws JsonProcessingException {
        
        ObjectMapper mapper = new ObjectMapper();
        
        //创建时间一个对象，java.util.Date
        Date date = new Date();
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(date);
        return str;
    }
    
    @RequestMapping("/json4")
    @ResponseBody
    public String json4() throws JsonProcessingException {
        
        ObjectMapper mapper = new ObjectMapper();
        
        //不使用时间戳的方式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        //自定义日期格式对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //指定日期格式
        mapper.setDateFormat(sdf);
        
        Date date = new Date();
        String str = mapper.writeValueAsString(date);
        
        return str;
    }
    
    @RequestMapping("/json5")
    @ResponseBody
    public String json5() throws JsonProcessingException {
        Date date = new Date();
        String json = JsonUtils.getJson(date);
        return json;
    }
}
